Skip to content

Bound Nectar's minute cron D1 reads and writes whatever history is stored - #15

Closed
zemaj wants to merge 1 commit into
mainfrom
strategy/d1-reads-nectar
Closed

zemaj wants to merge 1 commit into
mainfrom
strategy/d1-reads-nectar

Conversation

@zemaj

@zemaj zemaj commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Why

On Workers Free every tool on an account shares 5,000,000 D1 rows read and 100,000 rows written a day. Nectar's minute cron (audited at origin/main d378911, v0.1.1) read 919 rows a tick at six subscriptions and 19,542 at thirty subscriptions with one webhook endpoint (about 28 million a day), and wrote 74 to 111 rows a tick (about 160,000 a day).

What changed

  • Migration 0131: indexes that match the observers' queries (invoice by subscription and status, credit notes, allocations, usage credit applications, processor reversals, collections by invoice, subscriptions by customer, lifecycle by subscription), a partial index so the collection runner reads only collections that can still act, and sweep bookkeeping columns on the two observer cursors.
  • Subscription observer: a sweep runs on a new local day, hourly as a safety net, and at most every five minutes when the audit trail or usage ledger changed. Source writes still record their own state in their transaction. A source race clears the gate so the next tick retries.
  • Webhook entitlement observer: re-evaluates only customers named by new audit subjects (the customer, one of its subscriptions or invoices) or usage events, and sweeps on a new local day or hourly. Catalogue and policy edits that name no customer wait for that sweep. A feed position ahead of the ledger (a restore) triggers a full sweep.
  • Dispatcher: every run still gets a row; a job row is written only when the job changed something (a business audit event), failed, or ran over two seconds. Per-job heartbeat writes are gone (the timer heartbeat stays). History beyond seven days is trimmed, at most 100 runs an hour, so an old backlog never spends the write allowance.
  • job-runs door behaviour: a quiet minute now lists no jobs. tests/nectar/job-runs.mjs and API.md are updated for that; thrown failures still list the failed job and the unstarted ones after it.

Measurements (local D1, meta.rows_read / rows_written)

Same harness before and after: thirty subscriptions, sixty issued invoices, one webhook endpoint, the full minute job list.

before after
steady minute tick 19,542 read, 111 written 48 read, 6 written
six subscriptions, no endpoint 919 read, 74 written 44 read, 6 written
hourly sweep (thirty subscriptions + endpoint) every minute about 2,400 read, 70 written
a day (1,440 ticks + 24 sweeps) about 28.1M read, about 160k written about 0.13M read, about 10k written

The new cronBudgetFlow runs this through the emitted Worker with a metered D1 (thirty subscriptions, sixty invoices, an endpoint, thirty days of dispatcher history): quiet minute 48 read and 6 written, hourly sweep 2,435 read and 69 written. Ceilings are generous (150 / 20 quiet, 6,000 / 400 sweep, 500k / 30k a day).

Tests run locally

cronBudgetFlow, jobRunsFlow (updated), subscriptionStatesFlow, webhooksFlow, webhookLifecycleFlow, entitlementQuotasFlow, entitlementVisibilityFlow, automaticCollectionFlow, lifecycleFlow, dunningSequenceFlow, dunningCustodyFlow, paymentRetriesFlow, testModeFlow, nativeIntervalErasureFlow, plus typecheck, rer check, deployment-spec check and edition lock.

Not released: no version bump.

🤖 Generated with Claude Code

An idle minute tick read 919 rows at six subscriptions and 19,542 at thirty
subscriptions with a webhook endpoint (28 million rows a day against Workers
Free's 5 million), and wrote 74 to 111 rows a tick (over the 100,000-a-day
write limit). Invoice balances scanned every credit note, allocation and
collection, the subscription observer walked every subscription twice a
minute, the webhook observer re-evaluated every customer's entitlements every
minute, and the dispatcher wrote a row per job per run.

- Indexes that match the observers' queries (migration 0131), and a partial
  index so the collection runner reads only collections that can still act.
- The subscription observer sweeps on a new local day, hourly, and at most
  every five minutes on recorded activity; source writes still record their
  own state in their transaction.
- The webhook observer re-evaluates only customers named by new audit
  subjects or usage events, and sweeps on a new local day or hourly.
- The dispatcher records every run but writes a job row only when the job
  changed something, failed or ran over two seconds, and trims history older
  than seven days at most a hundred runs an hour.
- One read-budget test through the emitted Worker with a metered D1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zemaj

zemaj commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Closing because the same quiet minute returns seven completed job records on main and none here through the owner job-runs API.

@zemaj zemaj closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant